﻿layui.use(['table', 'laypage'], function () {
    var table = layui.table;
    var laypage = layui.laypage;
    var $ = layui.$;
    var layer = layui.layer; // 添加layui弹出层模块

    var size = 15; // 每页显示条数
    //获取列表
    function getListData(page) {
        //获取查询条件
        var DepartName = $('#DepartName').val();
        //ajax后台交互
        $.ajax({
            url: 'http://10.31.60.18:8000/AuthorizationGetRout/api/DepartmentShow/ArchivesPageList',
            type: 'get',
            dataType: 'json',
            //参数
            data: {
                page: page,
                size: size,
                DepartName: DepartName,
            },
            success: function (res) {
                //渲染表格数据
                table.render({
                    //挂载的表格id
                    elem: '#ID-table-demo-setRowChecked',
                    cols: [[
                        { type: 'checkbox', width: 60, fixed: 'center', unresize: true, align: 'center' },
                        { field: 'number', type: 'numbers', title: '序号', width: 60, fixed: 'center', unresize: true, align: 'center', templet: '#zizeng' },
                        { field: 'departCode', title: '部门编号', width: 120, align: 'center' },
                        { field: 'departName', title: '部门名称', width: 120, align: 'center' },
                        {
                            field: 'departtype', title: '部门类型', width: 120, align: 'center',
                            templet: function (data) {
                                if (data.departtype == 1) return '业务逻辑';
                                if (data.departtype == 2) return '生产加工';
                                if (data.departtype == 3) return '业务销售';
                            },
                        },
                        {
                            field: 'parentId', title: '部门状态', width: 120, align: 'center',
                            templet: function (data) {
                                if (data.parentId == 0) return '使用';
                                if (data.parentId == 1) return '禁用';
                            },
                        },
                        { field: 'departArea', title: '部门全称', width: 120, align: 'center' },
                        { field: '-', title: '部门主管', width: 120, align: 'center' },
                        { field: '-', title: '财务主管', width: 120, align: 'center' },
                        { field: 'createBy', title: '操作人', width: 120, align: 'center' },
                        { field: 'createDate', title: '操作时间', width: 120, align: 'center' },
                        { field: 'updateBy', title: '修改人', width: 120, align: 'center' },
                        { field: 'updateDate', title: '修改时间', width: 120, align: 'center' },
                        { title: '操作', width: 200, align: 'center', templet: '#barDemo', fixed: 'right', unresize: true },
                    ]],
                    toolbar: '#demo-toolbar-setRowChecked',
                    initSort: { // 设置初始排序
                        field: 'experience', // 字段名
                        type: 'asc' // 倒序
                    },
                    data: res.data
                });
                //分页
                laypage.render({
                    //挂载的分页id
                    elem: 'pagesbox',
                    count: res.tatalCount,
                    limit: size,
                    curr: page,
                    layout: ['count', 'prev', 'page', 'next', 'skip'],
                    jump: function (obj, first) {
                        if (!first) {
                            getListData(obj.curr);
                        }
                    }
                });
            }
        });
    }
    $('#searchBtn').on('click', function () {
        getListData(1);
    });
    // 单删除
    function deleteData(departId) {
        $.ajax({
            url: `http://10.31.60.18:8000/AuthorizationSetRout/api/Department/Shee_YandDel?departId=${departId}`,
            type: 'delete',
            success: function (res) {
                if (res > 0) {
                    layer.msg('删除成功');
                    getListData(1);
                } else {
                    layer.msg('删除成功');
                    getListData(1);
                }
            },
            error: function () {
                layer.msg('删除请求失败');
            }
        });
    }
    // 编辑删除按钮点击事件
    table.on('tool(ID-table-demo-setRowChecked)', function (obj) {
        var data = obj.data; // 获取当前行数据
        var departId = data.departId; // 获取当前行数据的唯一标识符
        if (obj.event === 'edit') {
            var url = `/Department/DepartmentUpd?departId=${departId}`;
            location.href = url; // 点击编辑按钮时跳转到修改页面
        }
        if (obj.event == 'delete') {
            layer.confirm('确定删除吗？', function (index) {
                deleteData(data.departId); // 传递正确的数据项ID
                layer.close(index);
            });
        }
    });
    // 初始化加载数据
    $(function () {
        getListData(1);
    })
});